package com.xinyan.zookeeper;

import com.xinyan.druid.MysqlDruid;
import com.xinyan.dto.DruidDto;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.util.concurrent.CountDownLatch;

import static com.xinyan.zookeeper.ZkConfig.PATH;

/**
 * @author xinyan.xie
 * @description
 * @date 2020/5/15
 */
public class UpdateNoteInfo {
    private static CountDownLatch watch = new CountDownLatch(1);

    private static CuratorFramework build = null;
    public static void main(String[] args) throws Exception {
                  build = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181")
                .namespace(PATH) //独立的命名空间，实现业务隔离
                .connectionTimeoutMs(500)
                .sessionTimeoutMs(500)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
        build.start();
        String path = "/URL";
        try {
            //1. 创建一个NodeCache
            NodeCache nodeCache = new NodeCache(build, path);

            //2. 添加节点监听器
            nodeCache.getListenable().addListener(() -> {
                ChildData childData = nodeCache.getCurrentData();
                if(childData != null){
                    System.out.println("Path: " + childData.getPath());
                    System.out.println("Stat:" + childData.getStat());
                    System.out.println("Data: "+ new String(childData.getData()));
                    System.out.println("节点"+PATH+"/URL"+"数据已发生变化："+build.getData().forPath("/URL").toString());
                    System.out.println("数据库连接信息已发生变化===========监听节点："+path);

                    String url = new String(build.getData().forPath("/URL"));
                    String driverClassName = new String(build.getData().forPath("/DRIVER_CLASS"));
                    String username = new String(build.getData().forPath("/USER_NAME"));
                    String password = new String(build.getData().forPath("/PASSWORD"));


                    System.out.println("url:"+url+"--"+"driverClass:"+driverClassName+"--"+"username:"+username+"--"+"password:"+password);
                    DruidDto druidDto = new DruidDto();
                    druidDto.setDriverClass(driverClassName);
                    druidDto.setPassword(password);
                    druidDto.setUrl(url);
                    druidDto.setUsername(username);
                    Connection connection = MysqlDruid.getConnection(druidDto);
                    System.out.println("当前数据库连接为："+connection);
                    System.out.println("重新初始化连接数据库连接");
                }
            });

            //3. 启动监听器
            nodeCache.start();

            //4. 返回NodeCache
        } catch (Exception e) {

        }

        System.out.println("nodeCache已开启监听模式===========监听节点："+path);

        build.setData().forPath( "/URL","jdbc:mysql://127.0.0.1:3306/springbootdata".getBytes());




        watch.await();




    }



}
